package com.hk.webapp.service.impl;

import com.hk.webapp.bean.CmsArticle;
import com.hk.webapp.bean.CmsCategory;
import com.hk.webapp.bean.SysUserArticleCate;
import com.hk.webapp.dao.CmsArticleMapper;
import com.hk.webapp.dao.CmsCategoryMapper;
import com.hk.webapp.dao.SysUserArticleCateMapper;
import com.hk.webapp.vo.CmsArticleQueryVo;
import com.hk.webapp.service.CmsArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true)
public class CmsArticleServiceImpl implements CmsArticleService {

    @Resource
    private CmsArticleMapper cmsArticleMapper;
    @Resource
    private CmsCategoryMapper cmsCategoryMapper;
    @Resource
    private SysUserArticleCateMapper sysUserArticleCateMapper;

    @Override
    public PageInfo<CmsArticle> searchPageList(CmsArticleQueryVo vo, Integer sysUserId) {
        List<CmsCategory> cateList=cmsCategoryMapper.getEntityList();
        List<Integer> cates=GetChild(cateList,vo.getCategory_id());
        if (vo.getCategory_id()>0){
            cates.add(vo.getCategory_id());
        }
        //判断分类是否是0 如是0则判断用户身份
        if (vo.getCategory_id()==0&& sysUserId!=1){
            SysUserArticleCate sysUserArticleCate= sysUserArticleCateMapper.selectBySysUserId(sysUserId);
            if (!Objects.isNull(sysUserArticleCate)&&!sysUserArticleCate.getArticleIds().equals("")){
                cates= Arrays.asList(sysUserArticleCate.getArticleIds().split(",")).parallelStream().map(Integer::valueOf).collect(Collectors.toList());
            }else{
                cates=Arrays.asList(0);
            }
        }
        PageHelper.startPage(vo.getPageIndex(),vo.getPageSize());
        List<CmsArticle> list=cmsArticleMapper.searchPageList(cates,vo.getKeyword());

        PageInfo<CmsArticle> plist=new PageInfo<>(list);

        return plist;
    }

    @Override
    public CmsArticle getEntityById(Integer id) {
        return cmsArticleMapper.selectByPrimaryKey(id);
    }

    @Override
    public CmsArticle getEntityByCode(String code, Integer notId) {
        return cmsArticleMapper.getEntityByCode(code,notId);
    }

    @Override
    @Transactional
    public int insert(CmsArticle cmsArticle) {
        return cmsArticleMapper.insertSelective(cmsArticle);
    }

    @Override
    @Transactional
    public int update(CmsArticle cmsArticle) {
        return cmsArticleMapper.updateByPrimaryKeySelective(cmsArticle);
    }

    @Override
    @Transactional
    public int softDelByIds(List<Integer> ids) {
        return cmsArticleMapper.softDelByIds(ids);
    }

    @Override
    public PageInfo<CmsArticle> getPageListByCateIdAndState(Integer pageIndex, Integer pageSize, Integer category, Integer state) {
        PageHelper.startPage(pageIndex,pageSize);
        List<CmsArticle> list=cmsArticleMapper.getPageListByCateIdAndState(category,state);
        PageInfo<CmsArticle> plist=new PageInfo<>(list);
        return plist;
    }

    @Override
    public PageInfo<CmsArticle> getPageListByCodeAndState(Integer pageIndex, Integer pageSize, String code, Integer state) {
        PageHelper.startPage(pageIndex,pageSize);
        List<CmsArticle> list=cmsArticleMapper.getPageListByCodeIdAndState(code,state);
        PageInfo<CmsArticle> plist=new PageInfo<>(list);
        return plist;
    }

    @Override
    public List<CmsArticle> getEntityListByRelId(Integer relId, Integer notId) {
        return cmsArticleMapper.getEntityListByRelId(relId,notId);
    }

    @Override
    public PageInfo<CmsArticle> searchPageListByKey(Integer pageIndex,Integer pageSize,List<Integer> cateIds, String key) {
        PageHelper.startPage(pageIndex,pageSize);
        List<CmsArticle> list=cmsArticleMapper.searchPageListByKey(cateIds,key);
        PageInfo<CmsArticle> plist=new PageInfo<>(list);
        return plist;
    }

    @Override
    public int getCount() {
        return cmsArticleMapper.getCount();
    }

    private List<Integer> GetChild(List<CmsCategory> list, Integer pid){
        List<Integer> ids=new ArrayList<>();
        for (CmsCategory category:list) {
            if (category.getParentId()==pid){
                ids.add(category.getId());
                ids.addAll(GetChild(list,category.getId()));
            }
        }
        return ids;
    }
}
